﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;

namespace WebRaoVat.Admin
{
    public partial class QuanLyTaiKhoan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LaAdmin"] == null || Session["LaAdmin"].ToString().Length <= 0)
            {                
                //Response.Redirect("../Default.aspx");
            }
        }

        /*protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            int maLoaiNDNew = LoaiNguoiDungBUS.getMa((string)e.NewValues[11]);

            int maND = (int)e.NewValues[0];
            int del = (int)e.NewValues[1];
            string tenND = (string)e.NewValues[2];
            string matKhau = (string)e.NewValues[3];
            string email = (string)e.NewValues[4];
            string dienthoai = (string)e.NewValues[5];
            string diachi = (string)e.NewValues[6];
            string maKichHoat = (string)e.NewValues[7];
            string tinhTrang = (string)e.NewValues[8];
            DateTime tgDK = (DateTime)e.NewValues[9];
            DateTime tgHetHan = (DateTime)e.NewValues[10];

            NguoiDungDTO nguoidung = new NguoiDungDTO(maND, tenND, matKhau, email, dienthoai, diachi,
                maKichHoat, tinhTrang, tgDK, tgHetHan, maLoaiNDNew, 0, del);

            if (NguoiDungBUS.Update(nguoidung))
                throw new InvalidOperationException("Cập nhật tài khoản thành công!");
            else
                throw new InvalidOperationException("Cập nhật tài khoản không thành công!");
        }*/

        /*protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            int maLoaiNDNew = LoaiNguoiDungBUS.getMa((string)e.NewValues[11]);

            //int maND = (int)e.NewValues[0];
            int del=0;
            try
            {
                del = (int)e.NewValues[1];
            }
            catch (Exception) {
            }

            string tenND=(string)e.NewValues[2];
            string matKhau=(string)e.NewValues[3];
            string email = (string)e.NewValues[4];
            string dienthoai = (string)e.NewValues[5];
            string diachi = (string)e.NewValues[6];
            string maKichHoat = (string)e.NewValues[7];
            string tinhTrang = (string)e.NewValues[8];
            DateTime tgDK = DateTime.Now;
            try
            {
                tgDK = (DateTime)e.NewValues[9];
            }
            catch (Exception) { }
            DateTime tgHetHan = DateTime.Now;
            try
            {
                tgHetHan = (DateTime)e.NewValues[10];
            }
            catch (Exception) { }

            NguoiDungDTO nguoidung = new NguoiDungDTO(-1, tenND, matKhau, email, dienthoai, diachi,
                maKichHoat, tinhTrang, tgDK, tgHetHan, maLoaiNDNew, 0, del);

            if (NguoiDungBUS.Insert(nguoidung))
                throw new InvalidOperationException("Thêm tài khoản thành công!");
            else
                throw new InvalidOperationException("Thêm tài khoản không thành công!");
        }*/

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int maLoaiNDNew = LoaiNguoiDungBUS.getMa((string)e.NewValues[10]);

            int maND = (int)e.Keys[0];

            int del = 1;
            try
            {
                del = int.Parse(e.NewValues[0].ToString());
            }
            catch (Exception)
            { }
            string tenND = (string)e.NewValues[1];
            string matKhau = (string)e.NewValues[2];
            string email = (string)e.NewValues[3];
            string dienthoai = (string)e.NewValues[4];
            string diachi = (string)e.NewValues[5];
            string maKichHoat = (string)e.NewValues[6];
            string tinhTrang = (string)e.NewValues[7];
            DateTime tgDK = DateTime.Parse(e.NewValues[8].ToString());
            DateTime tgHetHan = DateTime.Parse(e.NewValues[9].ToString());            

            NguoiDungDTO nguoidung = new NguoiDungDTO(maND, tenND, matKhau, email, dienthoai, diachi,
                maKichHoat, tinhTrang, tgDK, tgHetHan, maLoaiNDNew, 1, del);

            NguoiDungBUS.Update(nguoidung);
            Server.Transfer("quanlytaikhoan.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            NguoiDungDTO nguoidung = new NguoiDungDTO();
            NguoiDungBUS.InsertTemplate(nguoidung);
            Server.Transfer("quanlytaikhoan.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if ("" == txtLoaiNguoiDung.Text.Trim())
                return;
            LoaiNguoiDungDTO loaiNguoiDung = new LoaiNguoiDungDTO();
            loaiNguoiDung.TenLoaiNguoiDung = txtLoaiNguoiDung.Text.Trim();
            loaiNguoiDung.Deleted = 1;
            LoaiNguoiDungBUS.Insert(loaiNguoiDung);
            
            Server.Transfer("quanlytaikhoan.aspx");
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }  
    }
}
